<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="订单编号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="物流单号" prop="deliverySn">
        <el-input
          v-model="queryParams.deliverySn"
          placeholder="请输入物流单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人姓名" prop="receiverName">
        <el-input
          v-model="queryParams.receiverName"
          placeholder="请输入收货人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人电话" prop="receiverPhone">
        <el-input
          v-model="queryParams.receiverPhone"
          placeholder="请输入收货人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:order:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" border @selection-change="handleSelectionChange">
      <el-table-column label="订单id" align="center" prop="id" width="80"/>
      <el-table-column label="订单编号" align="center" prop="orderSn"/>
      <el-table-column label="订单总金额" align="center" prop="totalAmount"/>
      <!--      ：0-&gt;未支付；1-&gt;支付宝；2-&gt;余额支付-->
      <el-table-column label="支付方式" align="center" prop="payType">
        <template #default="{row,$index}">
          {{ row.payType === 0 ? '未支付' : row.payType === 1 ? '支付宝' : '余额支付' }}
        </template>
      </el-table-column>
      <!--：0-&gt;待付款；1-&gt;待发货；2-&gt;已发货；3-&gt;已完成；4-&gt;已关闭；5-&gt;无效订单-->
      <el-table-column label="订单状态" align="center" prop="status">
        <template #default="{row,$index}">
          {{
            row.status === 0 ? '待付款' : row.status === 1 ?
              '待发货' : row.status === 2 ? '已发货' : row.status === 3
                ? '已完成' : row.status === 4 ? '已关闭' : '无效订单'
          }}
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="createTime"/>
      <el-table-column label="收货人姓名" align="center" prop="receiverName"/>
      <el-table-column label="收货人电话" align="center" prop="receiverPhone"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="{row,$index}">
          <el-button
            size="small"
            @click="gotoDetail(row)"
            v-hasPermi="['order:order:showDetail']"
          >查看订单
          </el-button>
          <!--订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单-->
          <el-popconfirm v-if="row.status==0"
                         title="确定关闭该订单吗？"
                         @confirm="closeOrder(row)"
                         v-hasPermi="['order:order:close']"
          >
            <el-button slot="reference"  style="margin: 0 3px"  size="small"  type="danger">关闭订单</el-button>
          </el-popconfirm>

          <el-button
            v-if="row.status==1"
            size="small"
            v-hasPermi="['order:order:deliver']"
            @click="handleDeliver(row)"
          >订单发货
          </el-button>

          <el-button
            v-if="row.status==2||row.status==3"
            @click="showOrderafter"
            size="small"
          >订单跟踪
          </el-button>

          <el-button
            v-if="row.status==4"
            size="small"
            type="danger"
            @click="handleDelete(row)"
            v-hasPermi="['order:order:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <Orderafter :value="value" @input="input"></Orderafter>
  </div>
</template>

<script>
import {listOrder, getOrder, delOrder, addOrder, updateOrder} from "@/api/order/order";
import Orderafter from "./Orderafter/Orderafter"
export default {
  name: "Order",
  components:{
    Orderafter
  },
  data() {
    return {
      value:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单列表表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderSn: null,
        totalAmount: null,
        payAmount: null,
        freightAmount: null,
        promotionAmount: null,
        integrationAmount: null,
        couponAmount: null,
        discountAmount: null,
        payType: null,
        sourceType: null,
        status: null,
        deliveryCompany: null,
        deliverySn: null,
        autoConfirmDay: null,
        integration: null,
        growth: null,
        promotionInfo: null,
        receiverName: null,
        receiverPhone: null,
        receiverPostCode: null,
        receiverProvince: null,
        receiverCity: null,
        receiverRegion: null,
        receiverDetailAddress: null,
        note: null,
        confirmStatus: null,
        deleteStatus: null,
        useIntegration: null,
        paymentTime: null,
        deliveryTime: null,
        receiveTime: null,
        commentTime: null,
        modifyTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          {required: true, message: "$comment不能为空", trigger: "blur"}
        ],
        receiverName: [
          {required: true, message: "收货人姓名不能为空", trigger: "blur"}
        ],
        receiverPhone: [
          {required: true, message: "收货人电话不能为空", trigger: "blur"}
        ],
        deleteStatus: [
          {required: true, message: "删除状态：0-&gt;未删除；1-&gt;已删除不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //发货
    handleDeliver(row){
      this.$router.push({
        path: '/order/deliver',
        query: {
          orderList: JSON.stringify(row)
        }
      })
    },
    //订单跟踪
    showOrderafter() {
      this.value = true
    },
    input(val){
      this.value = val
    },
    closeOrder(row) {
      // 关闭订单
      row.status = 4;
      updateOrder(row).then(res => {
        if (res.code == 200) {
          this.getList();
          this.$message.success("已关闭订单")
        } else {
          this.$message.error("关闭订单失败")
        }

      })
    },
    gotoDetail(row){
      this.$router.push({
        path: '/order/showDetail',
        query: {
          id: row.id
        }
      })
    },
    /** 查询订单列表列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        couponId: null,
        orderSn: null,
        createTime: null,
        memberUsername: null,
        totalAmount: null,
        payAmount: null,
        freightAmount: null,
        promotionAmount: null,
        integrationAmount: null,
        couponAmount: null,
        discountAmount: null,
        payType: null,
        sourceType: null,
        status: null,
        deliveryCompany: null,
        deliverySn: null,
        autoConfirmDay: null,
        integration: null,
        growth: null,
        promotionInfo: null,
        receiverName: null,
        receiverPhone: null,
        receiverPostCode: null,
        receiverProvince: null,
        receiverCity: null,
        receiverRegion: null,
        receiverDetailAddress: null,
        note: null,
        confirmStatus: null,
        deleteStatus: null,
        useIntegration: null,
        paymentTime: null,
        deliveryTime: null,
        receiveTime: null,
        commentTime: null,
        modifyTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单列表编号为"' + ids + '"的数据项？').then(function () {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
